package com.codi.bus.core.resultModel;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.codi.base.util.BigDecimalUtil;
import com.codi.base.util.DateUtils;
import com.codi.base.util.MoneyUtils;
import com.codi.base.util.StringUtil;
import com.codi.bus.core.service.model.ShareDetailTotalModel;
import com.codi.bus.core.service.model.ShareModel;
import lombok.Getter;
import lombok.Setter;

public class ShareListResult extends BaseListResult {
	private static final long serialVersionUID = 1L;

	private List<ShareModel> shares;

	public void setShares(List<ShareModel> shares) {
		this.shares = shares;
	}

	public List<ShareModel> getShares() {
		return this.shares;
	}


    private List<ShareDetailTotalModel> shareDetailTotalModels;

	public void setShareDetailTotalModels(List<ShareDetailTotalModel> shareDetailTotalModels){
	    this.shareDetailTotalModels = shareDetailTotalModels;
    }

    public List<ShareDetailTotalModel> getShareDetailTotalModels(){return this.shareDetailTotalModels;}

	//总在途金额
	private BigDecimal totalInTransit;

    public BigDecimal getTotalInTransit() {
        return totalInTransit;
    }

    public void setTotalInTransit(BigDecimal totalInTransit) {
        this.totalInTransit = totalInTransit;
    }

    public String getTotalInTransitString(){
        if (totalInTransit != null && totalInTransit.doubleValue() != 0.00 ) {
            StringBuffer sb = new StringBuffer();
            sb.append("买入待确认");
            sb.append(MoneyUtils.formatMoney(totalInTransit));
            return sb.toString();
        }
        return "";
    }

    // 总持仓收益
	private BigDecimal totalAccumIncome;

	public void setTotalAccumIncome(BigDecimal totalAccumIncome) {
		this.totalAccumIncome = totalAccumIncome;
	}

	public BigDecimal getTotalAccumIncome() {
		return this.totalAccumIncome;
	}

	// 总持仓收益格式化字符串
	public String getTotalAccumIncomeString() {
		return MoneyUtils.formatMoney(this.totalAccumIncome, 2);
	}

	// 总本金
	private BigDecimal totalFundBalance;

	public void setTotalFundBalance(BigDecimal totalFundBalance) {
		this.totalFundBalance = totalFundBalance;
	}

	public BigDecimal getTotalFundBalance() {
		return this.totalFundBalance;
	}

	// 总本金格式化字符串
	public String getTotalFundBalanceString() {
		return MoneyUtils.formatMoney(this.totalFundBalance, 2);
	}

	// 总资产
	private BigDecimal totalWorthValue;

	public void setTotalWorthValue(BigDecimal totalWorthValue) {
		this.totalWorthValue = totalWorthValue;
	}

	public BigDecimal getTotalWorthValue() {
		return this.totalWorthValue;
	}

	// 总资产格式化字符串
	// 每个份额所显示的资产累加, 避免精度问题
	public String getTotalWorthValueString() {
		return MoneyUtils.formatMoney(this.totalWorthValue, 2);
	}

	//总持仓收益
    @Getter
    @Setter
    private BigDecimal totalShareIncome;

    public String getTotalShareIncomeString() {
        return MoneyUtils.formatMoney(totalShareIncome);
    }

	// 总当日收益
	private BigDecimal totalTodayIncome;

	public void setTotalTodayIncome(BigDecimal totalTodayIncome) {
		this.totalTodayIncome = totalTodayIncome;
	}

	public BigDecimal getTotalTodayIncome() {
		return this.totalTodayIncome;
	}

	// 总当日收益格式化字符串
	// 每个份额所显示的当日收益累加, 避免精度问题
	public String getTotalTodayIncomeString() {
		return getTotalTodayIncomeString(shares);
	}

	/**
	 * 获取总的当日收益，用每个份额所显示的当日收益累加，避免精度显示问题 e.g. fund-1: 662.25 fund-2: -24.92
	 * 但总的却是637.34(637.337), 所以界面显示时需要使用662.25+(-24.92)
	 *
	 * @param shares
	 * @return
	 */
	private String getTotalTodayIncomeString(List<ShareModel> shares) {
		BigDecimal total = new BigDecimal(0);
		if (shares != null) {
			for (ShareModel shareModel : shares) {
				if (!StringUtil.isEmpty(shareModel.getTodayIncomeString())) {
					total = MoneyUtils.add(total, BigDecimalUtil
							.ConvertToBigDecimal(MoneyUtils.unformatMoney(shareModel.getTodayIncomeString())));
				}
			}
		}

		return MoneyUtils.formatMoney(total);
	}

	/**
	 * 获取总资产
	 *
	 * @param shares
	 * @return
	 */
	private String getTotalWorthValueString(List<ShareModel> shares) {
		BigDecimal total = new BigDecimal(0);
		if (shares != null) {
			for (ShareModel shareModel : shares) {
				if (!StringUtil.isEmpty(shareModel.getWorthValueString())) {
					total = MoneyUtils.add(total, BigDecimalUtil
							.ConvertToBigDecimal(MoneyUtils.unformatMoney(shareModel.getWorthValueString())));
				}
			}
		}
		//在途中的金额也算总资产
        if(getTotalInTransit() != null) {
            total = MoneyUtils.add(total,getTotalInTransit());
        }
		return MoneyUtils.formatMoney(total);
	}

	@Setter
    @Getter
	private Integer totalNumInTransit;

    public String getTotalNumInTransitString() {
        if (totalNumInTransit != null && totalNumInTransit != 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("有");
            sb.append(totalNumInTransit);
            sb.append("笔买入确认中");
            return sb.toString();
        }
        return "";
    }


    /**
     * 净值日期
     */
    private Date netValueDate;

    @JSONField(serialize = false)
    public Date getNetValueDate() {
        return this.netValueDate;
    }

    public void setNetValueDate(Date netValueDate) {
        this.netValueDate = netValueDate;
    }

    // 净值日期字符串 MM-dd
    public String getNetValueDateString() {
        if (this.netValueDate != null) {
            return DateUtils.formatDate7(this.netValueDate);
        } else {
            return "";
        }
    }

}
